<?php
	
	require_once 'application/model.php';
    session_start();
	$user = $_SESSION['user'];
	$post = $_GET['id'];
	$isi = $_GET['isi'];
	$act = $_GET['act'];
	
	if($act==1) {
		$model = new Model();
		$model->newConnection( "localhost", "progin", "progin", "progin_171_13509027" );
		$queryUser = $model->cacheQuery("SELECT * FROM user WHERE username ='$user'");
		$queryComment = $model->cacheQuery("SELECT * FROM komentar");
			
		$_user = "";
		$row = 0;
		while($temp = $model->resultsFromCache( $queryUser ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$_user = $arr_row;
			$row++;
		}
		
		$model->executeQuery("INSERT INTO komentar(isi) VALUES('$isi')");
		$model->executeQuery("INSERT INTO dikomentar VALUES($post,(SELECT ID_komentar FROM komentar WHERE isi='$isi' ORDER BY waktu DESC LIMIT 1),'$user')");
		
		$_comm = array();
		while($temp = $model->resultsFromCache( $queryComment ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$_comm[] = $arr_row;
		}
		
		header('Content-type: text/xml');
		echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
		echo "<comments>";
		echo"<id>".count($_comm)."</id>";
		echo"<username>".$_user['username']."</username>";
		echo"<comment>".$isi."</comment>";
		echo"<avatar>".$_user['avatar']."</avatar>";
		echo "</comments>";
	} else if($act==-1) {
		echo "success";
	}
?>